package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程管理业务层实现类
 *
 * @Author:Mr.m
 * @Date: 2025-10-09 11:12
 * @Description: TODO
 */
@Service
public class CourseServiceImpl implements CourseService {
   @Autowired
    private CourseMapper courseMapper;

   //根据学科查询课程
    @Override
    public List<Course> queryCourseBySubject(Integer subject) {
        return courseMapper.queryCourseBySubject(subject);

    }

    //删除课程
    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    //修改课程
    @Override
    public void updateCourse(Course course) {
        //校验参数
        boolean hasNull = BeanUtil.hasNullField(course, "id", "description", "createTime", "updateTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        // 添加更新时间
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    //根据id查询课程（数据回显）
    @Override
    public Course queryCourseById(Integer id) {
        return courseMapper.queryCourseById(id);
    }

    //添加课程
    @Override
    public void addCourse(Course course) {
        //校验参数
        boolean hasNull = BeanUtil.hasNullField(course, "id", "description", "createTime", "updateTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }

//        //判断输入课程名称的长度
//        if (course.getName().length() > 20 || course.getName().length() < 2) {
//            throw new RuntimeException("课程名称长度必须在2-20之间");
//        }
//        //判断价格位数
//        if (course.getPrice().toString().length() > 5 || course.getPrice().toString().length() < 1) {
//            throw new RuntimeException("价格必须在1-5位之间");
//        }
//        //判断课程介绍长度
//        if(course.getDescription().toString().length()>100){
//            throw new RuntimeException("课程介绍长度不能超过100个字符");
//        }
        //补全参数
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());

        //插入数据
        courseMapper.insert(course);
    }

    // 查询所有课程
    @Override
    public List<Course> list() {
        return courseMapper.selectAll();
    }

    // 分页查询课程+
    @Override
    public PageResult<Course> page(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        //1.查询满足条件的总记录数
        Integer total = courseMapper.count(name, subject, target);
        //2.查询满足条件的当前页数据
        //计算偏移量
        Integer offset = (page - 1) * pageSize;
        List<Course> rows = courseMapper.selectByPage(name, subject, target, offset, pageSize);
        //3.数据组装
        return PageResult.<Course>builder()
                .total(total)
                .rows(rows)
                .build();

    }
}
